package com.sometip.locksreen;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class LockActivity extends Activity {

	DevicePolicyManager deviceManger;  
    ActivityManager activityManager;  
    ComponentName compName; 
    static final int RESULT_ENABLE = 1; 
    
    private Handler hdl;
	private long delayTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Runnable run = new Runnable() {			
			@Override
			public void run() {
				lockScreen();
				
			}
		};
		
        hdl = new Handler();
        delayTime = 3000;
        hdl.postDelayed(run, delayTime);
	}
	
	public void lockScreen() {        
        deviceManger = (DevicePolicyManager)getSystemService(  
                Context.DEVICE_POLICY_SERVICE);  
              activityManager = (ActivityManager)getSystemService(  
                Context.ACTIVITY_SERVICE);  
              compName = new ComponentName(this, LockAdmin.class);
    	
    	Intent intent = new Intent(DevicePolicyManager  
    		     .ACTION_ADD_DEVICE_ADMIN);  
    		            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,  
    		                    compName);  
    		            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,  
    		                    "Additional text explaining why this needs to be added.");  
    		            startActivityForResult(intent, RESULT_ENABLE);
    	
        boolean active = deviceManger.isAdminActive(compName);  
                 if (active) {  
                     deviceManger.lockNow();  
                 }  
		
	}

}
